package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 实现数据库的连接
 * 
 * @author 马远志
 * @date 2011-03-02
 * @version 1.0.0.0
 * 
 */
public class DBManager {
	/**
	 * 数据库驱动
	 */
	private static String driver;
	/**
	 * url：ip地址、数据库名
	 */
	private static String url;
	/**
	 * 数据库的用户名称
	 */
	private static String userName;
	/**
	 * 数据库的用户密码
	 */
	private static String userPassword;

	/**
	 * 单例模式
	 */
	static {
		driver = "com.mysql.jdbc.Driver";
		url = "jdbc:mysql://localhost:3306/test";
		userName = "root";
		userPassword = "111";
		// 将class文件加载
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public synchronized static Connection getConnection() throws SQLException {
		Connection conn = DriverManager.getConnection(url, userName,
				userPassword);
		return conn;
	}
	
	public static void main(String[] args) {
		try {
			System.out.println(getConnection());
		} catch (SQLException e) {			
			e.printStackTrace();
		}
	}
	

}
